package cn.fzhui;

import java.util.Scanner;

public class GobangGame {

    public static void main(String[] args) throws Exception {
        Chessboard chessboard = new Chessboard();
        chessboard.start();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            chessboard.printBoard();
            chessboard.printPlease();

            // 验证落子数据
            String input = scanner.next();
            if (validateInput(input)) {
                String[] strArr = input.split(",");
                int row = trans(strArr[0]);
                int column = trans(strArr[1]);

                if (chessboard.setElement(row, column)) {
                    if (chessboard.isWon(row, column)) {
                        break;
                    }
                    chessboard.changeChessman();
                }
            }
        }
    }

    /**
     * 验证输入参数
     *
     * @param input
     */
    public static boolean validateInput(String input) {
        String[] strArr = input.split(",");
        try {
            int row = trans(strArr[0]);
            int column = trans(strArr[1]);

            // 验证是否超出棋盘范围
            if (row > Chessboard.BOARD_SIZE || column > Chessboard.BOARD_SIZE) {
                printOutOfRange();
                return false;
            }

        } catch (Exception e) {
            printOutOfRange();
            return false;
        }

        return true;
    }

    /**
     * 数字转换
     *
     * @return
     */
    public static int trans(String string) throws Exception {
        switch (string) {
            case "a":
                return 10;
            case "b":
                return 11;
            case "c":
                return 12;
            case "d":
                return 13;
            case "e":
                return 14;
            case "f":
                return 15;
            default:
                return Integer.parseInt(string);
        }
    }

    /**
     * 打印超范围提醒
     */
    private static void printOutOfRange() {
        System.out.println("该位置超出棋盘范围！！！");
    }


}
